// session数据存储
global.session = {
  set: function (key, val) {
    sessionStorage.setItem(key, JSON.stringify(val))
  },
  get: function (key) {
    return JSON.parse(sessionStorage.getItem(key))
  },
  length: sessionStorage.length,
  remove: function (key) {
    sessionStorage.removeItem(key)
  },
  clear: function () {
    sessionStorage.clear()
  }
}

// 数据字典
global.dicCache = {
  /**
   * 获取指定mark的cache
   * @param {String} mark 字典标识
   */
  getCacheByMark: function (mark) {
    let cache = session.get ('session_dic_cache')
    return cache[mark]
  },
  /**
   * 获取指定mark和value的cache-value
   * @param {String} mark 字典标识
   * @param {String} name 字典名称
   */
  getValByMarkAndName: function (mark, name) {
    let dicCache = this.getCacheByMark(mark)
    if(dicCache && dicCache.length > 0) {
      let dic = dicCache.filter((x) => x.dicName === name)[0]
      return dic ? dic['dicVal'] : null
    } else {
      return null
    }
  },
  /**
   * 获取指定mode和value的cache-name
   * @param {String} mark 字典标识
   * @param {String} value 字典值
   */
  getNameByMarkAndVal: function (mark, value) {
    let dicCache = this.getCacheByMark(mark)
    if (dicCache && dicCache.length > 0) {
      let dic = dicCache.filter((x) => x.dicVal === value)[0]
      return dic ? dic['dicName'] : null
    } else {
      return null
    }
  },
  /**
   * 获取指定className的cache列表
   * @param {String} className
   */
  getCacheByClassName: function (className) {
    let cache = session.get("session_dic_cache")
    let allCache = []
    for (let i in cache) {
      allCache.push.apply(allCache, cache[i])
    }
    return allCache.filter((x) => x.className === className)
  },
  /**
   * 获取指定className和instanceName的cache列表
   * @param {String} className
   * @param {String} instanceName
   */
  getCacheByClassNameAndInstanceName: function (className, instanceName) {
    return this.getCacheByClassName(className).filter((x) => x.instanceName === instanceName)
  },
  /**
   * 通过className、instanceName和dicVal获取dicName
   * @param {String} className
   * @param {String} instanceName
   * @param {String} value
   */
  getNameByCIV: function (className, instanceName, value) {
    let dicCache = this.getCacheByClassNameAndInstanceName(className, instanceName)
    if (dicCache && dicCache.length > 0) {
      let dic = dicCache.filter((x) => x.dicVal === value)[0]
      return dic ? dic['dicName'] : null
    } else {
      return null
    }
  },
  /**
   * 通过className、instanceName和dicName获取dicVal
   * @param {String} className
   * @param {String} instanceName
   * @param {String} name
   */
  getValByCIN: function (className, instanceName, name) {
    let dicCache = this.getCacheByClassNameAndInstanceName(className, instanceName)
    if (dicCache && dicCache.length > 0) {
      let dic = dicCache.filter((x) => x.dicName === name)[0]
      return dic ? dic['dicVal'] : null
    } else {
      return null
    }
  }
}
